<?php
namespace control;
/*
*宿舍状态图类
*/


class dormitoryStateClass extends adIndexClass
{
    private const page = 'dormitoryState';
    private const title = '宿舍状态图';

    /*列表页-查询条件*/
    public function homeOptions($isAsync = true)
    {
        //生成查询表单
        $dormitory = query('dormitory',"1=1 order by time asc");
        //生成查询表单
        $html = $this->tableShow([
            [
                [
                    "楼号" => repeatSelect(array('sqlForm'=>"dormitory",'sqlField'=>'building','name'=>'building','value'=>$dormitory['building'])),
                    "类别"     => select(["name" => "type",'class' => 'text', 'option' => explode('，', para('dormitoryType'))]),
                    '状态' => select(array("name" => "state", "option" => ['100'=>'可入住','200'=>'住满','300'=>'维修/停用'])),
                ]
            ]
        ],1);
        //返回
        if(!$isAsync){
            return $html;
        }else{
            return json_encode(['html'=>$html],320);
        }
    }

    /*列表-查询条件*/
    public function home()
    {
        $html = $this->homeOptions(false);
        $button = "";
        //返回
        return $this->homeFun($this->search($html,layout: 'pop',toolButton: $button));
    }

    /*客户列表-查询结果*/
    public function homeSearch()
    {
        $building = $this->post('building');
        $where = '';
        if ($this->post('type')){
            $where.=" and type = '".$this->post('type')."'";
        }
        if ($this->post('state')){
            $where.=match ($this->post('state')){
                '100'=>" and maxPeople > (select ifnull(count(id),0) as total from dormitory_record where dormitoryId = dormitory.id and state = '入住')",
                '200'=>" and maxPeople <= (select ifnull(count(id),0) as total from dormitory_record where dormitoryId = dormitory.id and state = '入住')",
                default=>" and state in ('停用','维护中')"
            };
        }
        $floorAll = find(array('table'=>'dormitory','field'=>"distinct(floor) as floor",'where'=>"building = '".$building."' {$where} order by floor asc"));
        $tr = '';
        foreach ($floorAll['array'] as $array){
            $tr.=<<<eof
<tr><td>{$array['floor']}</td><td class="top-text"><ul class="dormitory-ul">
eof;
            $roomAll = find(array('table'=>'dormitory','where'=>"building = '".$building."' and floor = '$array[floor]' order by room asc"));
            foreach ($roomAll['array'] as $room){
                $livePeople = static::total(array('table'=>'dormitory_record','where'=>"dormitoryId = '$room[id]' and state = '入住'"));
                $people = find(array('table'=>"dormitory_record as a join staff as b on a.staffId = b.stid",'single'=>"a.dormitoryId = '$room[id]' and a.state = '入住' group by dormitoryId",'field'=>"group_concat(b.name) as name"));
                $people['name'] = $people['name']??'-';
                $color = 'green-li';
                if($room['state'] !='启用'){
                    $color = 'gray';
                }elseif ($livePeople >=$room['maxPeople']){
                    $color = 'purple';
                }
                $tr.=<<<eof
                    <li class="$color">
                        <div>{$room['room']}&emsp;{$room['type']}</div>
                        <div>可住/已住：{$room['maxPeople']}/{$livePeople}</div>
                        <div>{$people['name']}</div>
                    </li>
eof;
            }
            $tr.="</ul></td></tr>";
        }

        //返回
        $json['html'] = <<<eof
<div class="dormitory-title"><span class="building-title">{$building}</span><span class="to-left"><div class="dot green-li"></div>&nbsp;可入住</span>&emsp;&emsp;<span><div class="dot purple"></div>&nbsp;住满</span>&emsp;&emsp;<span><div class="dot gray"></div>&nbsp;维修停用</span></div>
<div class="dormitory-content">
    <table>
        {$tr}
    </table>
</div>
<style>
.top-text{vertical-align: top}
.dormitory-content>table{border-collapse: collapse;border-spacing: 0;table-layout: fixed}
.dormitory-content>table tr td:first-child{width: 80px;height: 200px}
.dormitory-content>table tr td{border: 1px solid rgba(215, 215, 215, 1);}
.dormitory-title{height: 60px;line-height: 60px;font-weight: 400;font-size: 14px}
.building-title{font-weight: 700;font-size: 18px}
.dot{display: inline-block;width: 20px;height: 20px;border-radius: 50%;vertical-align: text-bottom;}
.green-li{background-color: #7EDC84}
.gray{background-color: #D7D7D7}
.purple{background-color: #7C72FA}
.to-left{margin-left: 50px}
.dormitory-ul li{float: left;color: white;padding: 7px;width: 180px;height: 90px;text-align: left;border-radius: 4px;margin-left: 5px;margin-top: 5px}
</style>
eof;
        return json_encode($json, 320);
    }

}

